package com.takepoints21.mobile;

import com.takepoints.android.TPConst;
import com.takepoints.android.services.SecurityServices;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class UserLogin extends BaseActivity {
	
	@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.login);
        manageMenu(0);
        
        Bundle bundle = this.getIntent().getExtras();
    	final String pageSrc = bundle.getString("pageSrc");
    	
    	TextView lnkRegister = (TextView)findViewById(R.id.lnkRegister);
    	lnkRegister.setOnClickListener(new View.OnClickListener(){
			 public void onClick(View view) {
				 Intent regisInt = new Intent(view.getContext(), UserRegister.class);
				 startActivityForResult(regisInt, 0);
			 }
    	});
    	
    	final EditText txtUsrname = (EditText)findViewById(R.id.txtUsrname);
    	final EditText txtPasswd = (EditText)findViewById(R.id.txtPasswd);
    	
    	String prefUsrname = getSharedStringValue(TPConst.PRF_USRNAME);
    	if(prefUsrname != null && prefUsrname.trim().length() > 0){
	    	txtUsrname.setText(prefUsrname);
	    	txtUsrname.setSelection(txtUsrname.getText().length());
    	}
    	
    	String prefPasswd = getSharedStringValue(TPConst.PRF_PASSWD);
    	if(prefPasswd != null && prefPasswd.trim().length() > 0){
    		txtPasswd.setText(prefPasswd);
    		txtPasswd.setSelection(txtPasswd.getText().length());
    	}
    	
    	Button btnAccess = (Button)findViewById(R.id.btnAccess);
    	btnAccess.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View view) {
				 String uname = txtUsrname.getText().toString().toLowerCase();
				 String paswd = txtPasswd.getText().toString();
				 
				 int memId = SecurityServices.verifyUser(uname, paswd);
				 if(memId > 0){
					 Editor editor = sharePref.edit();
					 editor.putString(TPConst.PRF_USRNAME, uname);
					 editor.putString(TPConst.PRF_PASSWD, paswd);
					 editor.putInt(TPConst.PRF_MEMBER_ID, memId);
					 editor.commit();
					 
					 popup("Login Success");
					 
					 Intent settingInt = null;
					 if("SETTING".equals(pageSrc)){
						 settingInt = new Intent(view.getContext(), Setting.class);
					 } else{
						 settingInt = new Intent(view.getContext(), Takepoints.class);
					 }
					 startActivityForResult(settingInt, 0);
				 } else{
					 popup("Login failed, Please try again !");
				 }
			}
    	});
    	/*
    	ImageView btnFbLogin = (ImageView)findViewById(R.id.btnFbLogin);
    	btnFbLogin.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View view) {
    			Intent fbAppsInt = new Intent(view.getContext(), FacebookApps.class);
    			fbAppsInt.putExtra("FB_EVENT", "LOGIN");
	    		startActivityForResult(fbAppsInt, 0);
    		}
    	});*/
	}
	
	@Override
	public void onBackPressed(){
		Intent homeInt = new Intent(this, Takepoints.class);
		startActivityForResult(homeInt, 0);
		return;
	}
	
}
